package com.java46.struts2.user.dao;

import com.java46.struts2.base.exception.BusinessException;
import com.java46.struts2.base.util.HibernateUtils;
import com.java46.struts2.base.entity.TUserEntity;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query;

import java.util.List;

/**
 * @ClassName TUserDao
 * @Description 用户数据逻辑
 * @Author zuxia
 * @Date 2018/7/17 10:13
 * @Vcrsion 1.0
 **/
public class TUserDao {


    /**
     * @Author xiaobo
     * @Description //查询用户是否存在
     * @Date 10:17 2018/7/17
     * @Param [uSign]
     * @return TUserEntity
     */
    public static TUserEntity queryTUserUsign(String Usign){
        if (Usign==null||Usign.trim().equals("")){
            throw new BusinessException("用户标识不存在");
        }
        Query query=HibernateUtils.getQuery("FROM TUserEntity WHERE uSign='"+Usign+"'");
        List resultList = query.getResultList();
        if(resultList.isEmpty()){
            return null;
        }
        return (TUserEntity) resultList.get(0);
    }

    /**
     * @Author xiaobo
     * @Description //新增用户
     * @Date 10:58 2018/7/17
     * @Param [Usign]
     * @return void
     */
    public static void addTUser(String uSign,String uName){
        Session session=HibernateUtils.getSession();
        Transaction transaction=session.beginTransaction();
        TUserEntity tuser=new TUserEntity();
        tuser.setuSign(uSign);
        tuser.setuName(uName);
        tuser.setuId(0);
        session.save(tuser);
        transaction.commit();
        session.close();
    }
}
